package com.exonline.back.controller.info;

import com.exonline.commons.entity.OnlineInfo;
import com.exonline.commons.dao.DaoImpl;
import com.exonline.commons.dao.IDao;
import org.apache.commons.dbutils.handlers.ArrayListHandler;
import org.apache.commons.dbutils.handlers.MapListHandler;

import javax.servlet.http.HttpServletRequest;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @desc 数据分析模块M层
 * @author hbrj
 */

public class InfoService {
  IDao dao = new DaoImpl();//引入数据持久化层

  /**
   * @desc 1.获取数据分析模块的相关数据指标信息
   * @author hbrj
   */
  public OnlineInfo getOnlineInfo(HttpServletRequest req) throws SQLException {
      //1.初始化数据分析对象
      OnlineInfo onlineInfo = new OnlineInfo();
      int totalUser = 0;
      int onlineUser = 0;
      String totalProfit;
      int totalCourses = 0;

      //2.获取全部课程信息
      totalCourses = dao.queryCount("SELECT count(*) FROM courses");
      List<Object[]> list = dao.queryWithPagingAndConditions("SELECT * FROM totalprofit", null, 1, 100, new ArrayListHandler(), null);
      onlineInfo.setTotalCourses(totalCourses);//2.1将获取到的全部课程信息装入到onlineInfo对象中


      //3.获取今日收益
      totalProfit = String.valueOf(list.get(0)[0]);
      if(totalProfit==null||totalProfit.equals("null")){
          onlineInfo.setTotalProfit("0");
      }else {
          onlineInfo.setTotalProfit(totalProfit);//3.1 将获取到的今日收益装入onlineInfo对象中
      }


      //4.获取各学科方向占比
      //4.1通过查询各学科方向占比视图,获取到各学科方向占比的详细信息
      List<Object[]> cdirectionList = dao.queryWithPagingAndConditions("SELECT * FROM cdirectionview", null, 1, 10, new ArrayListHandler(), null);
      Map<String, Object> cdirectionMap = new HashMap<>();
      //4.2创建map容器,将获取到的各学科方向(cdirection),百分比(precentage),如 前端方向,16.95%
      for (Object[] objects : cdirectionList) {
          cdirectionMap.put(objects[0].toString(), objects[2].toString());
      }
      onlineInfo.setPercentage(cdirectionMap);


      //5.获取各学科科目占比
      //5.1通过查询各学科科目占比视图,获取到各学科科目占比的详细信息
      List<Object[]> csubjectList = dao.queryWithPagingAndConditions("SELECT * FROM subjectview", null, 1, 100, new ArrayListHandler(), null);
        //5.2创建map容器,将获取到的各学科科目(csubject),百分比(subjectprecentage),如Vue.js,10.34%
      Map<String, Map<String, Object>> coursedmap = new HashMap<>();
      for (Object[] objects : csubjectList) {
          String cdirection = (String) objects[0];
          String csubject = (String) objects[1];
          String csubjectPrecentage = String.valueOf(objects[3]);
          if (coursedmap.get(cdirection) == null) {
              coursedmap.put(cdirection, new HashMap<>());
              coursedmap.get(cdirection).put(csubject, csubjectPrecentage);
          } else {
              coursedmap.get(cdirection).put(csubject, csubjectPrecentage);
          }
      }
      System.out.println(coursedmap);
      onlineInfo.setSubjectPercentage(coursedmap);


      //6.获取每周销售趋势
      //6.1查询销售趋势视图,获取每周销售趋势详细信息
      List<Map<String, Object>> trendsWeekMap = dao.queryWithPagingAndConditions("SELECT * FROM trendsweekview", null, 1, 10, new MapListHandler(), null);
      onlineInfo.setTrendsweek(trendsWeekMap);//6.2 将获取到的每周销售趋势装入到onlineInfo对象中
      System.out.println(trendsWeekMap);

      //7.获取年销售趋势
      //7.1查询年销售趋势视图,获取年r内销售趋势详细信息
      List<Map<String, Object>> trendsMonthMap = dao.queryWithPagingAndConditions("SELECT * FROM trendsyearview", null, 1, 10, new MapListHandler(), null);
      onlineInfo.setTrendsmonth(trendsMonthMap);//7.2将获取到的年内销售趋势详细信息装入到onlineInfo对象中
      System.out.println(trendsMonthMap);
      System.out.println(totalUser + "," + onlineUser + "," + totalProfit + "," + totalCourses);

      //8.为了降低难度并保证演示效果,我们随机产生一些在线用户数,
      onlineInfo.setTotalUser(dao.queryWithPagingAndConditions("SELECT * FROM student",null,1,9999,new MapListHandler(),null).size()*7);
//      onlineInfo.setOnlineUser((int)(Math.random() * 100));
      onlineInfo.setOnlineUser((Integer) req.getServletContext().getAttribute("onlineUser"));
      return onlineInfo;
  }
}
